package com.example.navigation;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.example.TukelaExampleDemo.R;
import com.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GuideActivity extends Activity implements OnPageChangeListener {
	private ViewPager vp;
	private ViewPagerAdapter vpAdapter;
	private List<View> views;
	private LinearLayout ll;
	//底部小点图片
	private ImageView[] dots;
	//记录当前选中位置
	private int currentIndex;

	private Map<Integer, View> viewMap = new HashMap<Integer, View>();
	//引导图片资源
	private static final int images_slide[] =
			{R.drawable.slide1,
					R.drawable.slide2,
					R.drawable.slide3,
					R.drawable.slide4
			};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide);

		// 初始化页面
		initViews();

		// 初始化底部小点
		initDots();
	}

	private void initDots() {
		// TODO Auto-generated method stub
		ll = (LinearLayout) findViewById(R.id.ll);
		dots = new ImageView[views.size()];
		//循环取得小点图片
		for(int i = 0; i<images_slide.length;i++)
		{
			dots[i] = (ImageView)ll.getChildAt(i);
			// 下面小点，初始化所有项都使能状态       
			dots[i].setEnabled(true);
			//设置位置tag，方便取出与当前位置对应
			dots[i].setTag(i);
		}
		currentIndex = 0;
		dots[currentIndex].setEnabled(false);//设置为白色，即选中状态。

	}

	private void setCurrentDot(int pos)
	{
		if(pos <0 || pos>views.size()-1 || currentIndex == pos)
		{
			return;
		}

		dots[pos].setEnabled(false);
		dots[currentIndex].setEnabled(true);
		currentIndex = pos;
	}
	private void setCurrentView(int position)
	{
		if (position < 0 || position >= images_slide.length) {
			return;
		}

		vp.setCurrentItem(position);
	}

	private void initViews() {
		// TODO Auto-generated method stub

		views = new ArrayList<View>();

		for(int i=0;i< images_slide.length;i++)
		{
			View view = getView(i,images_slide[i]);
			views.add(view);
		}

		//初始化adapter
		vpAdapter = new ViewPagerAdapter(views,this);

		vp = (ViewPager) findViewById(R.id.vPager);
		vp.setAdapter(vpAdapter);
		//绑定回调
		vp.setOnPageChangeListener(this);


	}

	public View getView(int arg0,int image)
	{
		View rowView = this.viewMap.get(arg0);

		if (rowView == null) {

			LayoutInflater inflater = this.getLayoutInflater();
			rowView = inflater.inflate(R.layout.guide_item, null);
			ImageView image_item = (ImageView)rowView.findViewById(R.id.image);
			image_item.setBackgroundResource(image);

			viewMap.put(arg0, rowView);
		}
		return rowView;
	}

	// 当滑动状态改变时调用  
	public void onPageScrollStateChanged(int arg0) {
	}

	// 当当前页面被滑动时调用
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	// 当新的页面被选中时调用
	public void onPageSelected(int arg0) {
		// 设置底部小点选中状态
		setCurrentDot(arg0);
	}








}
